package com.xy_pro.xymanager.auth.security;

import com.xy_pro.xymanager.entity.UserEntity;
import com.xy_pro.xymanager.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class DataInitializer implements CommandLineRunner {
    @Autowired
    public UserRepository userRepository;
    @Autowired
    public PasswordEncoder passwordEncoder;
    @Override
    public void run(String... args) throws Exception {
        // 检查是否已有管理员
        if (userRepository.findByRole(UserEntity.Role.ROLE_ADMIN).isEmpty()) {
            UserEntity admin = new UserEntity();
            admin.setName("admin");
            admin.setPhone("13888888888");
            admin.setPassword(passwordEncoder.encode("sa123456"));
            admin.setRole(UserEntity.Role.ROLE_ADMIN);
            userRepository.save(admin);
        }
    }
}
